<template>
    <div class="page">
        <header class="header">
            <div class="header-font">我的金币</div>
            <div class="header-wrap">
                <div class="header-price">{{ userInfo.integral }}</div>
                <div class="header-but">去获取</div>
            </div>
        </header>
        <div class="content">
            <div class="content-header" @click="handInfo">
                <div class="content-price">立减金</div>
                <div class="content-info">查看详情</div>
            </div>
            <img v-if="knockList.length === 0" @click="handCart" src="@/assets/images/1.3.png" alt="">
            <div v-else class="price-img mt-xs" v-for="(data, index) in knockList" :key="index">
                <div class="price-left">
                    <span class="price-wrap">￥<span>{{ data.coupon_price }}</span></span>
                </div>
                <div class="price-right">
                    <div class="price-top">立减金</div>
                    <div class="price-bot">无门槛立减</div>
                </div>
            </div>
        </div>
        <!-- 特权卡 -->
        <!-- <div class="content content-wrap">
            <div class="content-header"  @click="handCard">
                <div class="content-price">特权卡</div>
                <div class="content-info">查看详情</div>
            </div>
            <div class="content-position">
                <div class="content-img-flex" @click="handUserHelping">
                    <div class="content-img-font">砍价卡</div>
                </div>
                <div class="content-wrapper" v-if="!userInfo.bargaining || userInfo.bargaining === 0">
                    <p>{{ parseInt(this.userInfo.integral) - parseInt(this.bargainInfo.value) > 0 ? '可以兑换特权卡' 
                        : `还差${Math.abs(parseInt(this.userInfo.integral) - parseInt(this.bargainInfo.value))}个金币即可兑换` }}</p>
                    <div class="content-con" @click="handConversion">
                        {{ parseInt(this.userInfo.integral) - parseInt(this.bargainInfo.value) > 0 ? '立即兑换' : `马上赚金币` }}
                    </div>
                </div>
            </div>
        </div> -->
        <div class="content content-wrap">
            <div class="content-header" @click="handPrivilege">
                <div class="content-price">特权金</div>
                <div class="content-info">查看详情</div>
            </div>
            <div v-for="(data,index) in (privilegeList.length ? privilegeList : 1)" :key="index" @click="handPrivilegeInfo(data.cid)" style="position: relative">
                <div class="content-wrapper" v-if="!privilegeList.length">
                    <p>还差99个金币即可兑换</p>
                    <div class="content-con">马上赚金币</div>
                </div>
                <div style="position: relative;" class="w-full">
                    <img src="@/assets/images/1.1.png" alt="coupon-background">
                    <div style="position: absolute; top: 0; bottom: 0; margin: auto" class="w-full type-flex justify-between font-color-white">
                        <div class="coupon-money"><span>￥</span><span style="font-size: .66rem;">{{ parseInt(data.coupon_price) }}</span></div>
                        <div class="type-flex justify-between">
                            <div>
                                <div><span v-if="data.ned_point" class="font-bold">{{ data.ned_point }}</span> {{ data.ned_point ? `金币兑换` : `限单首用，除拼团` }}</div>
                                <div class="font-xs" v-if="data.countdown">{{ `${data.countdown.day > 0 ? data.countdown.day+':' : '' }${data.countdown.hour}:${data.countdown.min}:${data.countdown.sec}.${data.countdown.ms}` }}后作废</div>
                                <div class="font-xxs">多款特权金标志商品{{ data.ned_point ? `支持` : `首单` }}使用</div>
                            </div>
                            <div><span class="btn-sm">立即使用</span></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
</template>

<script>
    import { getUserInfo, getChangeList, getReduction, ExchangeDiscount, getCouponsUser, myChangeList } from "@/api/user"
    import { countdown } from "@utils/common.js";
    export default {
        data() {
            return {
                userInfo: '',
                bargain: [],
                bargainInfo: '',
                dataIndex: 0,
                knockList: [],
                privilegeList: [],
                timer: null,
            }
        },
        mounted() {
            this.init();
        },
        destroyed() {
            if (this.privilegeList.length) {
                clearInterval(this.timer);
            }
        },
        methods: {
            init() {
                this.getUserInfoPort();
                this.getChangeListPort();
                this.getReductionPort();
                this.getCouponsUserPort();
                this.myChangeListPort();
            },
            /**
             * 金币兑换列表
             */
            async getChangeListPort() {
               const data = await getChangeList();
               this.bargain = data.data;
               this.bargain.forEach(item => {
                   if (item.type === '1') {
                       this.bargainInfo = item;
                   }
               });
            },
            /**
             * 查看立减金
             */
            async getReductionPort() {
                const data = await getReduction().catch(error => {
                    this.knockList = [];
                });
                this.knockList = data ? data.data : [];
            },
            /**
             * 特权金接口
             */
            async getCouponsUserPort() {
                const data = await getCouponsUser(1).catch(error => console.error(error));
                if (!data.data.length) return;
                for(let key in data.data) {
                    data.data[key].countdown = {};
                }
                this.privilegeList = data.data;
                let that = this, ms = 10;
                this.timer = setInterval(() => {
                    ms--;    
                    if (ms === -1) {
                        ms = 9;
                    }
                    for(let key in that.privilegeList) {
                        let endTime = Number(that.privilegeList[key].end_time+'000');
                        that.privilegeList[key].countdown = countdown(endTime, ms);
                    }
                }, 100);
            },
            /**
             * 用户信息
             */
            async getUserInfoPort() {
                try {
                    const data = await getUserInfo();
                    this.userInfo = data.data;
                } catch (error) {
                    console.log(error);
                }
            },
            /**
             * 特权卡接口
             */
            async myChangeListPort() {
               const data = await myChangeList().catch(error => console.error(error));
            //    console.log(data);
            },
            /**
             * 兑换卡片
             */
            async getExchangeDiscount(cname, point) {
                const data = await ExchangeDiscount(cname, point).catch(error => {
                    console.error(error);
                });
                this.$dialog.success(data.msg);
                await this.getChangeListPort();
                // this.$router.go(0);
            },
            /**
             * 立即兑换按钮点击
             * cname = membership参团，makeup拼团，bargaining砍价
             */
            handConversion() {
                if (parseInt(this.userInfo.integral) - parseInt(this.bargainInfo.value) > 0) {
                    this.getExchangeDiscount('bargaining', parseInt(this.bargainInfo.value));
                }
            },
            /**
             * 跳转到一元购
             */
            handCart() {
                this.$router.push('activity/goods_panic_buy')
            },
            /**
             * 立减金
             */
            handInfo() {
                this.$router.push('userWelfareInfo/1')
            },
            /**
             * 特权金
             */
            handPrivilege() {
                this.$router.push('userWelfareInfo/2')
            },
            /**
             * 特权卡
             */
            handCard() {
                this.$router.push('userWelfareInfo/3')
            },
            /**
             * 特权列表页
             */
            handPrivilegeInfo(id) {
                this.$router.push({ path: `/activity/list?type=privilege&id=${id}` })
            },
            /**
             * 特权卡商品列表
             */
            handUserHelping() {
                this.$router.push('userHelping')
            }
        }
    }
</script>

<style scoped>
    .page {
        min-height: 100vh;
        background: rgb(245,245,245);
    }

    .header {
        width: 100%;
        height: 170px;
        background: linear-gradient(to bottom left, rgb(227,43,170), rgb(115,95,246));
        border-bottom-left-radius: 200px 20px;
        border-bottom-right-radius: 200px 20px;
        padding: 20px;
    }
    .header-font {
        color: rgb(247,211,126);
        font-size: 15px;
        margin-bottom: 10px;
    }

    .header-wrap {
        display: flex;
        align-items: center;
    }
    .header-price {
        font-size: 40px;
        color: #fff;
        margin-right: 10px;
    }

    .header-but {
        width: 70px;
        height: 25px;
        background: rgb(245,189,65);
        color: #fff;
        border-radius: 50px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 12px;
    }

    .content {
        width: 90%;
        margin: 0 auto;
        height: auto;
        background: #fff;
        border-radius: 6px;
        margin-top: -50px;
        padding: 10px;
        margin-bottom: 10px;
    }

    .content-header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;
    }

    .content-price {
        font-weight: 700;
    }

    .content-info {
        font-size: 12px;
        color: rgb(161,161,161);
    }

    .content-wrap {
        margin-top: 0;
    }

    .content-position {
        width: 100%;
        height: 80px;
        position: relative;;
    }

    .content-img-flex {
        width: 100%;
        height: 80px;
        background: url("../../assets/images/2.1.png") no-repeat;
        background-size: 100% 100%;
        position: relative;
    }

    .content-img-font {
        font-size: 21px;
        color: #fff;
        position: absolute;
        top: 24px;
        left: 23px;
    }

    .content img {
        width: 100%;
        height: 80px;
    }

    .content-wrapper {
        width: 100%;
        height: 80px;
        background: rgba(0,0,0,.6);
        position: absolute;
        top: 0;
        left: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        border-radius: .06rem;
        z-index: 3;
    }

    .content-wrapper p {
        color: #ffffff;
        font-size: 12px;
        font-weight: 700;
        margin-bottom: 5px;
    }

    .content-con {
        border: 1px solid #ffffff;
        color:#fff;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50px;
        padding: 3px 7px;
        font-size: 12px;
        font-weight: 700;
    }
    .content-img {
        width: 100%;
        height: 80px;
        background: url("../../assets/images/1.1.png") no-repeat;
        background-size: 100%;
        position: relative;
        margin-bottom: 10px;
    }
    .img-price {
        position: absolute;
        left: 34px;
        font-size: 30px;
        color: #fff;
        top: calc(50% + 3px);
        transform: translateY(-50%);
    }

    .img-font {
        position: absolute;
        color: rgb(255,234,242);
        left: 86px;
        top: 7px;
        font-size: 14px;
    }

    .img-time {
        position: absolute;
        color: rgb(255,234,242);
        left: 86px;
        top: 28px;
        font-size: 12px;
    }

    .img-text {
        width: 120px;
        top: 42px;
    }

    .price-img {
        width: 100%;
        height: 80px;
        background: linear-gradient(to left, rgb(251,99,85), rgb(253,79,75));
        display: flex;
    }

    .price-left {
        width: 30%;
        display: flex;
        align-items: center;
        justify-content: center;
        border-right: 1px solid rgb(254,199,186);
    }

    .price-left span {
        color: #fff;
        font-size: 30px;
        font-weight: 700;
    }

    .price-left .price-wrap {
        font-size: 16px;
    }

    .price-right {
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding-left: 10px;
    }

    .price-top {
        color: #fff;
    }

    .price-bot {
        color: #fff;
    }

    .coupon-money {
        padding: 0 4%;
        margin-right: 3%;
        border-right: 1px dashed #ffe8d4;
    }


    .btn-sm {
        display: block;
        min-width: 1.6rem;
        padding: .14rem .26rem;
        margin-right: .3rem;
        text-align: center;
        font-size: .2rem;
        color: rgba(255,124,78,1);
        letter-spacing: .02rem;
        background-color: #fff;
        border-radius: 5rem;
        box-shadow: 2px 2px 10px rgb(253,79,75);
    }
</style>